package com.example.demo.innoket.generate;

import com.example.demo.innoket.util.*;
import com.example.demo.innoket.vo.Table;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * java 代码模板
 * @author zhangLi
 */
public class PHPGenerate implements Generate {
	FileType fileType = null;

	public PHPGenerate(FileType fileType) {
		this.fileType = fileType;
	}

	@SuppressWarnings("static-access")
	public void generate(Table table, GeneratorConfig.ModulePath path) throws Exception {
		if (fileType == null)
			throw new Exception(PHPGenerate.class.getName() + ": JavaFileType 为null");
		/* get the Template */
		Template t = new VelocityUtil().getTemplate(Resources.TEMPLATE_PATH+"/"+fileType.getTemplate());
		/* create a context and& nbsp;add data */
		VelocityContext context = new VelocityContext();
		String base = path.daoPath.replace("\\","_").split("_")[0]+"_";
		String baseName = path.daoPath.replace("\\","_");//life_car
		context.put("baseName", Underline2Camel.underline2Camel(baseName,false));//LifeCar
		context.put("excludeCol",GeneratorConfig.EXCLUDE_COL);
		//获取实例对象名
		String tableName = table.getTableName().toLowerCase();
		StringBuilder sb = new StringBuilder();
		String[] arrs = tableName.split("_");
		if(arrs!=null && arrs.length>0){
			int len = arrs.length;
			if (len > 1) {
				for (int i = 1; i < len; i++) {
					String s = arrs[i];
					sb.append(Character.toUpperCase(s.charAt(0)));
					sb.append(s.substring(1));
				}
			}
		}
		String entityInstance= ConvertHandler.getEntityInstance(tableName);//lifeCarBusInfo
		context.put("entityInstance", entityInstance);//lifeCarBusInfo

		String className = ConvertHandler.table2clazzName(tableName.split(base)[1]);
		System.out.println("className =" +className);
		context.put("entityName", className);//CarBusInfo
		context.put("tableName", tableName);//life_car_bus_info
		String routeBase = tableName.split(base)[1];
		if(routeBase.indexOf("_") > 0){
			context.put("route", routeBase.split("_")[0]);//car
			context.put("routeName", routeBase.replace("_","-"));//car-bus-info
		}else{
			context.put("route", routeBase);//car
			context.put("routeName", routeBase);//car
		}
		context.put("lowerEntityName",Underline2Camel.toLowerCaseFirstOne(className));//carBusInfo;

		context.put("upperEntityName",Underline2Camel.camel2Underline(className).toUpperCase());//CAR_BUS_INFO

		context.put("columns", table.getColumns());
		context.put("author", Resources.AUTHOR);//作者
		context.put("idStratage", GeneratorConfig.ID_GENERATE_STRATGE);
		context.put("dateType", ConvertHandler.getDateType(table.getColumns()));//判断时间类型
		context.put("timestampType", ConvertHandler.getTimestampType(table.getColumns()));//判断时间类型
		context.put("bigDecimal", ConvertHandler.getBigDecimalType(table.getColumns()));//判断浮点数类型

		//获取系统时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		context.put("createfiletime", sdf.format(new Date()));
    	
		/* now render the template into a StringWriter */
		StringWriter writer = new StringWriter();
		t.merge(context, writer);
		System.out.println(Resources.BASE_PACKAGE );
		System.out.println(path.getPath(fileType));
//		System.out.println(className + fileType.getFileNameExtension());

		if(FileType.ROUTE.equals(fileType)){
			FileUtil.create(Resources.BASE_PACKAGE , FileType.ROUTE.getType() + fileType.getFileNameExtension(), writer.toString());
		}else{
			FileUtil.create(Resources.BASE_PACKAGE + path.getPath(fileType), className + fileType.getFileNameExtension(), writer.toString());
		}
	}

	public void generate(List<Table> tables,GeneratorConfig.ModulePath path) throws Exception {

	}

}
